package com.common.dialect;

public class SqlServerDialect extends Dialect {

	@Override
	public String getLimitString(String sql, int offset, int limit) {
		sql = sql.trim();
		boolean isForUpdate = false;
		if (sql.toLowerCase().endsWith(" for update")) {
			sql = sql.substring(0, sql.length() - 11);
			isForUpdate = true;
		}

		StringBuffer pagingSelect = new StringBuffer(sql.length() + 100);
		
		pagingSelect.append("select * from ( select row_number()over(order by tempcolumn)temprownumber,* from ( select top "+
							(offset+limit)+" tempcolumn=0,* from ( ");
		
		pagingSelect.append(sql);
		
		pagingSelect.append(") t )tt )ttt where temprownumber> "+offset);

		if (isForUpdate) {
			pagingSelect.append(" for update");
		}
		
		return pagingSelect.toString();
	}

}
